//
//  GDGInfiniteScrollView.m
//  QoilPublic
//
//  Created by SunChao on 17/9/13.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "GDGInfiniteScrollView.h"
#import "CustomTableView.h"
#import "OrderListModel.h"

@interface GDGInfiniteScrollView()<UIScrollViewDelegate>
@property(weak,nonatomic)UIScrollView * scrollView;
@property (nonatomic,strong)CustomTableView *myTableView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@end
@implementation GDGInfiniteScrollView

-(instancetype)initWithFrame:(CGRect)frame dataSource:(NSArray *)dataSource
{
    if (self = [super initWithFrame:frame]) {
        //滚动视图
        UIScrollView * scrollView = [[UIScrollView alloc]init];
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.pagingEnabled = YES;
        scrollView.bounces = NO;
        scrollView.delegate = self;
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        self.dataSource = [NSMutableArray arrayWithArray:dataSource];
        
        for (int i = 0; i <dataSource.count; i++) {
            OrderListModel *model  = [dataSource objectAtIndex:i];

            NSString *orderId = model.orderId;
            NSString *address = model.adress;
            NSString *statusTime = model.stateTime;
            NSString *stateText = model.stateText;
            NSString *purId = model.purId;
            NSString *integral = model.integral;
            _myTableView = [[CustomTableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-StatusBarHeight) orderId:orderId orderAddress:address statusTime:statusTime stateText:stateText purId:purId integral:integral];
            _myTableView.tag = i;
            
            [scrollView addSubview:_myTableView];
            OrderListModel *model1 = [self.dataSource objectAtIndex:0];
            [self requestIsLook:model1.purId];
        }
        
    }
    
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.scrollView.frame = self.bounds;
    if (self.isScrollDirectionPortrait) {
        self.scrollView.contentSize = CGSizeMake(0, self.dataSource.count * self.bounds.size.height);
    }else{
        self.scrollView.contentSize = CGSizeMake(self.dataSource.count * self.bounds.size.width, 0);
    }
    
    for (int i = 0; i<self.dataSource.count; i++) {
        CustomTableView * tbView = self.scrollView.subviews[i];
        if (self.isScrollDirectionPortrait) {
            tbView.frame = CGRectMake(0, i * self.scrollView.frame.size.height, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
            
        }else
        {
            tbView.frame = CGRectMake(i*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        }
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int index = scrollView.contentOffset.x/kScreenWidth;
    OrderListModel *model =  [self.dataSource objectAtIndex:index];
    [self requestIsLook:model.purId];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView  {
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    NSLog(@"滑动");
}

- (void)requestIsLook:(NSString *)purId{
    //for (QMsgListModel * model in _dataArr) {
    //    [_puidArr addObject:model.purId];
    //  }
    //WS(wSelf);
    // NSString *purIds = [_puidArr componentsJoinedByString:@","];
    NSString *url = [QParamService urlWithAction:@"addPushIsLook"];
    NSDictionary *param = [QParamService addPushIsLookWithPurId:purId];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            CLog(@"标记为已读");
            //if (wSelf.block) {
            //  wSelf.block();
            //}
        }else{
        }
    }];
    
}
@end
